package com.rgst.controller;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rgst.comm.SearchParam;
import com.rgst.dao.entity.SecUser;
import com.rgst.dao.repository.SecUserRepository;
import com.rgst.dto.Result;
import com.rgst.utils.Gssert;
import com.rgst.utils.Md5SaltTool;
import com.rgst.utils.VoUtils;

/**
 * Created by tancw on 2019/12/8.
 */
@Controller
@RequestMapping("secUser")
public class SecUserController {

    @Autowired
    private SecUserRepository secUserRepository;

    @RequestMapping("list")
    public String list() {
        return "rgst/sec/list";
    }

    @RequestMapping("list-data")
    @ResponseBody
    public Page<SecUser> listData(SearchParam sp) {
        Pageable pageable = sp.getPageable(new Sort(Sort.Direction.ASC, "id"));
        ExampleMatcher matcher = ExampleMatcher.
                matching()
                .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.startsWith())
                .withMatcher("realname", ExampleMatcher.GenericPropertyMatchers.startsWith())
                .withIgnorePaths("id", "password", "status", "createTime");
        return secUserRepository.findAll(sp.getParam(SecUser.class, matcher), pageable);
    }

    @RequestMapping("form")
    public String toForm() {
        return "rgst/sec/form";
    }

    @RequestMapping("toUpdate")
    public String toForm(Long id, Model model) {
        model.addAttribute("info", secUserRepository.findById(id).get());
        return "rgst/sec/update";
    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(SecUser user) {
        Assert.notNull(user);
        Gssert.notEmpty(user.getUsername(),"用户名不能为空");
        Assert.notNull(user.getPassword(),"密码不能为空");

        SecUser oldUser = secUserRepository.findByUsername(user.getUsername());
        Assert.isNull(oldUser, "用户已存在");
        try {
            user.setCreateTime(new Date());
            user.setPassword(Md5SaltTool.getEncryptedPwd(user.getPassword()));
            secUserRepository.save(user);
            return Result.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增失败");
        }
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(SecUser user) {
        Assert.notNull(user);
        Assert.notNull(user.getId());
        SecUser old = secUserRepository.findById(user.getId()).get();
        Assert.notNull(old, "用户记录不存在");
        try {
            if (StringUtils.isBlank(user.getPassword())) {
                user.setPassword(old.getPassword());
            }
            user.setPassword(Md5SaltTool.getEncryptedPwd(user.getPassword()));
            VoUtils.mergeTo(user, old);
            secUserRepository.save(old);
            return Result.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("操作失败");
        }
    }
}
